package com.be.beadmin.material.util;

import com.be.beadmin.common.entity.SortExpressEntity;
import com.be.beadmin.common.utils.JpaSortBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

public class JpaSortBuilderJoin extends JpaSortBuilder {
    public static <Z,X> List<Order> getPredicateJoin(Join<Z,X> join, List<SortExpressEntity> sortExpressEntities, CriteriaBuilder cb) {
        List<Order> list = new ArrayList();
        if (sortExpressEntities == null) {
            return list;
        } else {
            for(Integer i = 0; i < sortExpressEntities.size(); i = i + 1) {
                SortExpressEntity item = (SortExpressEntity)sortExpressEntities.get(i);
                Order order;
                if (item.getDesc() != null && item.getField() != null && item.getDesc()) {
                    order = cb.desc(join.get(item.getField()));
                    list.add(order);
                } else {
                    order = cb.asc(join.get(item.getField()));
                    list.add(order);
                }
            }

            return list;
        }
    }


}
